package utils;


import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author: feiwang_6
 * @create: 2020/9/24 20:40
 * @description: 远程文件下载
 */
public class DownloadFileUtil {

    /**
     * 下载远程文件url到指定目录
     * @param dir
     * @param fileUrl 远程文件地址 https://dl-cdn.oray.com/sunlogin/windows/SunloginClient_10.5.0.29613_X64.exe
     * @throws IOException
     */
    public static void downloadFile(String dir, String fileUrl) throws IOException {
        URL url = new URL(fileUrl);
        File localFile = new File(dir + parsePath(fileUrl));
        FileUtils.copyURLToFile(url, localFile);
    }

    /**
     * 通过URL
     * @param url
     * @return
     */
    private static String parsePath(String url){
        String fileName = url.substring(url.lastIndexOf("/"));
        return fileName;
    }
    
    public static void main(String[] args) {
        String url = "http://fms-oss.oss-cn-hangzhou.aliyuncs.com/intelli-video/115041042882669/2020-09-24/115041042882669_200924024342_62_1.mp4";
        //String url = "https://dl-cdn.oray.com/sunlogin/windows/SunloginClient_10.5.0.29613_X64.exe";
        String localDir = "D:/";
        try {
            downloadFile(localDir, url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
